package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.pojo.Role;
import com.zoshine.zzg.pojo.pojostatus.CommonStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色Controller
 *
 * @author 田沛凌
 * @time 2017年6月22日 下午4:10:01
 */
@RestController
@RequestMapping(value = "role", method = RequestMethod.POST)
public class RoleController {
    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 增加角色
     *
     * @param role 角色信息
     */
    @RequestMapping("/addRole")
    public BaseResponse addRole(Role role) {
        roleService.add(role);
        return AppResponse.OK;
    }

    /**
     * 删除角色
     *
     * @param id 角色编号
     */
    @RequestMapping("/delRole")
    public BaseResponse delRole(Long id) {
        roleService.del(id);
        return AppResponse.OK;
    }

    /**
     * 修改角色
     *
     * @param role 角色信息
     */
    @RequestMapping("/editRole")
    public BaseResponse editRole(Role role) {
        roleService.edit(role);
        return AppResponse.OK;
    }

    /**
     * 获取角色信息
     *
     * @param id 角色编号
     */
    @RequestMapping("/getRole")
    public BaseResponse getRole(Long id) {
        Role role = roleService.get(id);
        return new AppResponse(role);
    }

    /**
     * 获取角色分页列表
     *
     * @param role 查询参数
     * @return 角色分页列表
     */
    @RequestMapping("/pageRole")
    public BaseResponse pageRole(Role role) {
        List<Role> roleList = roleService.list(role);
        return new AppResponse(new PageInfo<>(roleList));
    }

    /**
     * 获取角色列表
     *
     * @param role 查询参数
     * @return 角色列表
     */
    @RequestMapping("/listRole")
    public BaseResponse listRole(Role role) {
        List<Role> roleList = roleService.list(role);
        return new AppResponse(roleList);
    }

    /**
     * 获取角色状态列表
     * @return 角色状态列表
     */
    @RequestMapping("/listRoleStatus")
    public BaseResponse listRoleStatus() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommonStatus commonStatus : CommonStatus.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", commonStatus.getValue());
            map.put("text", CommonStatus.getDesc(commonStatus.getValue()));
            list.add(map);
        }
        return new AppResponse(list);
    }
}
